import socket

# 创建TCP套接字
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接服务器
server_address = ('localhost', 8888)
client_socket.connect(server_address)
print(f"已连接到服务器 {server_address[0]}:{server_address[1]}")

try:
    while True:
        # 发送消息给服务器
        message = input("客户端说: ")
        client_socket.send(message.encode('utf-8'))

        # 接收服务器响应
        data = client_socket.recv(1024)
        if not data:
            break
        print(f"服务器回答: {data.decode('utf-8')}")

finally:
    # 关闭连接
    client_socket.close()